﻿/*
 * Author: Alexander Sjösten
 * Thomas Mattsson wrote SaveToDisk and GetCustomerStrings
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Final_Project.CustomerData
{
    /// <summary>
    /// This class represents the Customer manager of the program.
    /// It is responsible for all interaction with Customer objects.
    /// </summary>
    public class CustomerManager
    {
        // Local variables
        private FileManager fileManager;
        private List<Customer> customers;
        private const int firstID = 1000;


        /// <summary>
        /// Constructor for the CustomerManager, will create the
        /// list that will contain all customers and set the id
        /// to the id default value
        /// </summary>
        public CustomerManager()
        {
            fileManager = new FileManager();
            customers = fileManager.DeserializeCustomers();
        }


        /// <summary>
        /// AddCustomer will add a customer to the customers list
        /// </summary>
        /// <param name="customer">The customer to add</param>
        public void AddCustomer(Customer customer)
        {
            if (customers.Count == 0)
            {
                SetCustomerID(customer, firstID);
                customers.Add(customer);
            }
            else
            {
                SetCustomerID(customer, customers[customers.Count - 1].ID + 1);
                customers.Add(customer);
            }

            SaveToDisk();
        }


        /// <summary>
        /// Method to set an ID to a specific customer (broke it out
        /// as a method since it's used in both if and else branch,
        /// copy-paste is nasty ;-) )
        /// </summary>
        /// <param name="customer">The customer to set the ID to</param>
        /// <param name="id">The ID to give to the customer</param>
        private void SetCustomerID(Customer customer, int id)
        {
            customer.ID = id;
            SaveToDisk();
        }


        /// <summary>
        /// Method to remove a customer at a given index
        /// </summary>
        /// <param name="index">The index of the customer to be removed</param>
        public void RemoveCustomer(int index)
        {
            if (CheckIndex(index))
            {
                try
                {
                    customers.RemoveAt(index);
                }
                catch (ArgumentOutOfRangeException)
                {
                }
                finally
                {
                    SaveToDisk();
                }
            }
        }


        /// <summary>
        /// Method to remove a given customer from the list
        /// </summary>
        /// <param name="customer">Customer to be removed</param>
        public void RemoveCustomer(Customer customer)
        {
            bool resultOfRemoval = customers.Remove(customer);
            if (resultOfRemoval)
                SaveToDisk();
        }


        /// <summary>
        /// This method updates the list at index to the
        /// new customer.
        /// </summary>
        /// <param name="index">List index to update.</param>
        /// <param name="customer">Customer object to save at index.</param>
        public void UpdateCustomer(int index, Customer customer)
        {
            if (CheckIndex(index))
            {
                customers[index] = customer;
                SaveToDisk();
            }
        }



        /// <summary>
        /// This method creates an array of strings from
        /// the list of customers.
        /// </summary>
        /// <returns>Array of strings about customers.</returns>
        public string[] GetCustomerStrings()
        {
            string[] result = new string[customers.Count];

            for (int i = 0; i < customers.Count; i++)
            {
                result[i] = customers[i].ToString();
            }

            return result;
        }


        /// <summary>
        /// This method gets a specific Customer from the customers list.
        /// </summary>
        /// <param name="index">Index of the customer to get.</param>
        /// <returns>Customer if index is within bounds, null otherwise.</returns>
        public Customer GetCustomer(int index)
        {
            if (CheckIndex(index))
            {
                Customer customer = customers[index];
                return customer;
            }
            return null;
        }


        /// <summary>
        /// This method calls fileManager to save the contents
        /// of the customer list to permanent storage.
        /// </summary>
        private void SaveToDisk()
        {
            fileManager.SerializeCustomers(customers);
            
        }


        /// <summary>
        /// This method checks that index is within bounds of
        /// customers list.
        /// </summary>
        /// <param name="index">The index to check</param>
        /// <returns>True if index is within bounds, False otherwise.</returns>
        private bool CheckIndex(int index)
        {
            return (index >= 0 && index < customers.Count);
        }
    }
}
